package com.gem.growingtree.utils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.util.Log;

import com.gem.growingtree.activity.vo.User;

public class HttpUtil {

	// 创建HTTPClient对象
	public static HttpClient httpClient = new DefaultHttpClient();
	public static final String urlPath = "http://10.204.1.45:8080/growTree/";
	protected static final String TAG = "HtpUtil";

	/**
	 * @param url
	 *            发送请求的URL
	 * @return 服务器响应字符串
	 * @throws Exception
	 */
	public static String getRequest(final String url) throws Exception {
		FutureTask<String> task = new FutureTask<String>(
				new Callable<String>() {
					@Override
					public String call() throws Exception {
						// 创建HTTPGet对象
						HttpGet get = new HttpGet(url);
						// 发送Get请求
						HttpResponse httpResponse = httpClient.execute(get);
						// 如果服务器成功的返回响应
						if (httpResponse.getStatusLine().getStatusCode() == 200) {
							// 获取服务器响应字符串
							String result = EntityUtils.toString(httpResponse
									.getEntity());
							return result;
						}
						return null;
					}
				});
		new Thread(task).start();
		return task.get();
	}

	/**
	 * @param url
	 *            发送请求的URL
	 * @return 服务器响应字符串
	 * @throws Exception
	 */
	public static String postRequest(final String url,
			final Map<String, Object> rawParams) throws Exception {
		FutureTask<String> task = new FutureTask<String>(
				new Callable<String>() {
					@Override
					public String call() throws Exception {
						HttpClient client = new DefaultHttpClient();
						// String path = urlPath+"ws/register/main";
						HttpPost httpPost = new HttpPost(url);
						List<NameValuePair> parameters = new ArrayList<NameValuePair>();

						for (String key : rawParams.keySet()) {
							parameters.add(new BasicNameValuePair(key,
									rawParams.get(key).toString()));
							System.out.println(new BasicNameValuePair(key,
									rawParams.get(key).toString()).toString());
						}

						// User user = (User) rawParams.get("user");
						//
						// parameters.add(new BasicNameValuePair("email", user
						// .getEmail()));
						// parameters.add(new BasicNameValuePair("nickName",
						// user
						// .getNickName()));
						// parameters.add(new BasicNameValuePair("cellPhone",
						// user
						// .getCellPhone()));
						// parameters.add(new BasicNameValuePair("password",
						// user
						// .getPassword()));
						// parameters.add(new BasicNameValuePair("address", user
						// .getAddress()));
						// parameters.add(new BasicNameValuePair("role", user
						// .getRole()));
						httpPost.setEntity(new UrlEncodedFormEntity(parameters,
								"utf-8"));
						Log.i("AA", "AA52222222222222A");

						HttpResponse response = client.execute(httpPost);
						Log.i("AA", "AA55655AAA");
						int responseCode = response.getStatusLine()
								.getStatusCode();
						if (responseCode == 200) {
							InputStream is = response.getEntity().getContent();
							String result = new String(StreamTool
									.readInputStream(is), "utf-8");
							Log.i("AA", "AAAccccccAA" + result);
							return result;
						} else {
							Log.i("AA", "A A1211111111111111111");
							return null;
						}
					}
				});
		new Thread(task).start();
		return task.get();
	}
}
